package com.xbongbong.saas.enums.subform;

import com.xbongbong.paas.enums.FieldTypeEnum;
import com.xbongbong.saas.enums.business.DistributorAccountEnum;
import lombok.Getter;

import java.util.Objects;

/**
 *
 * @author lee
 * @date 2020/8/25 09:58
 * @since
 * @version
 */
@Getter
public enum DistributorAccountSubFormEnum {
    /**
     *
     */
    DISTRIBUTOR_ID("distributorId", DistributorAccountEnum.DISTRIBUTOR_NAME.getAttr(), FieldTypeEnum.NUM.getType()),
    NAME("name", DistributorAccountEnum.NAME.getAttr(), FieldTypeEnum.TEXT.getType()),
    PHONE("phone", DistributorAccountEnum.PHONE.getAttr(), FieldTypeEnum.TEXT.getType()),
    ROLE("role", DistributorAccountEnum.AUTHORITY.getAttr(), FieldTypeEnum.NUM.getType()),
    ENABLE("enable", DistributorAccountEnum.ACCOUNT_STATUS.getAttr(), FieldTypeEnum.NUM.getType()),

    ;
    /**
     * 实体attr
     */
    private final String dbAttr;
    /**
     * data内attr
     */
    private final String dataAttr;
    /**
     * 字段类型，详见
     *
     * @see FieldTypeEnum
     */
    private final Integer fieldType;

    DistributorAccountSubFormEnum(String dbAttr, String dataAttr, Integer fieldType) {
        this.dbAttr = dbAttr;
        this.dataAttr = dataAttr;
        this.fieldType = fieldType;
    }


    public static String getAttrConnectData(DistributorAccountSubFormEnum cache) {
        return "data." + cache.getDataAttr();
    }

    public static String getEsAttr(DistributorAccountSubFormEnum cache) {
        FieldTypeEnum fieldTypeEnum = FieldTypeEnum.getFieldTypeEnum(cache.getFieldType());
        //字段类型为text，或者字段在es内的类型时text，则需要在后面拼上“.keyword”
        boolean flag = fieldTypeEnum == FieldTypeEnum.TEXT || "text".equals(fieldTypeEnum.getEsalias());
        if (flag) {
            return "data." + cache.getDataAttr() + ".keyword";
        }
        return "data." + cache.getDataAttr();
    }

    public static String getEsAttrByDbAttr(String dbAttr) {
        for (DistributorAccountSubFormEnum value : values()) {
            if (Objects.equals(dbAttr, value.getDbAttr())) {
                return getEsAttr(value);
            }
        }
        return null;
    }

    public static String getAttrConnectDataByDbAttr(String dbAttr) {
        for (DistributorAccountSubFormEnum value : values()) {
            if (Objects.equals(dbAttr, value.getDbAttr())) {
                return getAttrConnectData(value);
            }
        }
        return null;

    }
}
